//
//  HomeViewController.m
//  Kwapro
//
//  Created by Leafc on 13-11-3.
//  Copyright (c) 2013年 Leafc. All rights reserved.
//

#import "HomeViewController.h"
#import "HMSegmentedControl.h"
#import "SignInViewController.h"
#import "QuestionListViewController.h"

@interface HomeViewController () {
    CGFloat headHeight;
    QuestionListViewController *_questionListVC;
}

@property (strong, nonatomic) HMSegmentedControl *segmented;
@property (strong, nonatomic) UIScrollView *scrollView;

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0) {
            headHeight = 20 + 44;
        }
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect frame = CGRectMake(0, headHeight, self.view.frame.size.width, 44);
	self.segmented = [[HMSegmentedControl alloc] initWithFrame:frame];
    self.segmented.selectionIndicatorColor = [UIColor colorWithRed:51.0f  / 255.0f
                                                             green:102.0f / 255.0f
                                                              blue:0.0f   / 255.0f
                                                             alpha:1.0f];
    self.segmented.selectionIndicatorMode = HMSelectionIndicatorFillsSegment;
    self.segmented.sectionTitles = @[@"RSS新闻", @"最新问题", @"热门标签"];
    [self.segmented addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmented];
    
    self.scrollView = [[UIScrollView alloc]
                       initWithFrame:CGRectMake(0,
                                                headHeight + self.segmented.frame.size.height,
                                                self.view.frame.size.width,
                                                self.view.frame.size.height - self.segmented.frame.size.height)];
    self.scrollView.backgroundColor = [UIColor grayColor];
    
    
    _questionListVC = [[QuestionListViewController alloc] init];
    [self.scrollView addSubview:_questionListVC.view];
    [self.view addSubview:_scrollView];
    
   
    UIButton *btnSignIn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 200,
                                                                     self.view.frame.size.height - 50,
                                                                     200, 50)];
    btnSignIn.backgroundColor = [UIColor orangeColor];
    [btnSignIn setTitle:@"登录" forState:UIControlStateNormal];
    [btnSignIn addTarget:self action:@selector(btnSignInAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSignIn];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI Actions

- (void)segmentedValueChanged:(HMSegmentedControl *)sender
{
    CGPoint contentOffset = self.scrollView.contentOffset;
    contentOffset.x = self.segmented.selectedIndex * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:contentOffset animated:YES];
}

- (void)btnSignInAction:(UIButton *)sender
{
    SignInViewController *viewController = [[SignInViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
